package middleware

import (
	"github.com/starmotioncf/common/log"
	"github.com/starmotioncf/soutchatinternal"
	"github.com/starmotioncf/soutchatinternal/service"
	"go.uber.org/zap"

	"github.com/gin-gonic/gin"
)

// MaskChinaMiddleware 屏蔽大陆用户
type MaskChinaMiddleware struct {
	restrictionService service.RestrictionService
}

func NewMaskChinaMiddleware(restrictionService service.RestrictionService) *MaskChinaMiddleware {
	return &MaskChinaMiddleware{
		restrictionService: restrictionService,
	}
}

func (m *MaskChinaMiddleware) Handle(ctx *gin.Context) {
	if m.restrictionService.MaskChinaUser(ctx) {
		log.WithContext(ctx).Info("检查到大陆用户禁止访问",
			zap.String("ip", ctx.ClientIP()),
			zap.String("mac", internal.GetMacFromCtx(ctx)),
			zap.Uint64("uid", internal.GetUidFromCtx(ctx)))
		return
	}
	ctx.Next()
}
